﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EncuentraloSilverlight.ServiceReference2;
using System.Windows.Navigation;
using System.Windows.Browser;
using System.IO;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Collections;
using System.Collections.ObjectModel;

namespace EncuentraloSilverlight
{
    public partial class EditProduct : Page
    {
        public EditProduct()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string parameter = Cookie.GetCookie("SelectedProduct");
            string[] splitParameter = parameter.Split(',');
            textboxNombreProducto.Text = splitParameter[0];
            textboxPrecioProducto.Text = splitParameter[1];
            textboxDescripcionProducto.Text = splitParameter[2];

        }
        
        private void Guardar_Cambios(object sender, RoutedEventArgs e)
        {            
            int precio = -1;
            if (textboxNombreProducto.Text.Length != 0 && textboxPrecioProducto.Text.Length != 0
                && textboxDescripcionProducto.Text.Length != 0 && int.TryParse(textboxPrecioProducto.Text, out precio))
            {
                Service2Client webservice = new Service2Client();

                webservice.FindSellerFromNameCompleted += (obj, eventarg) =>
                {
                    /*
                    if (image_bytes != null)
                    {
                        Binary bin = new Binary();
                        bin.Bytes = image_bytes;
                        producto.Image = bin;
                    }
                     */

                    //Datos Producto antiguo
                    int precioantiguo=-1;
                    string parameter = Cookie.GetCookie("SelectedProduct");
                    string[] splitParameter = parameter.Split(',');
                    int.TryParse(splitParameter[1],out precioantiguo);
                    
                    //Creamos un producto nuevo
                      
                    Product modifiedProduct = new Product()
                        {
                            Name = textboxNombreProducto.Text,

                            Price = precio,

                            Description = textboxDescripcionProducto.Text
                        };
                    modifiedProduct.SellerID = eventarg.Result[0].SellerID;
                   

                    //Sacamos todos los producos
                    ObservableCollection<Product> products = new ObservableCollection<Product>();
                    webservice.FindAllProductsCompleted += (obj1, eventarg1) =>
                        {
                             products = eventarg1.Result;

                             Product productToModify = new Product();
                             for (int i = 0; i < products.Count; i++)
                             {
                                 if (products[i].Name.Equals(splitParameter[0]) && products[i].Price == precioantiguo && products[i].Description.Equals(splitParameter[2]))
                                 {
                                     productToModify = products[i];
                                     break;
                                 }
                             }
                             //Lo modificamos
                             webservice.ModifyProductCompleted += (obj2, eventarg2) =>
                             {
                                 MessageBox.Show("Producto Modificado con exito");
                                 NavigationService.Navigate(new Uri("/Views/Seller_View.xaml", UriKind.Relative));
                             };
                             webservice.ModifyProductAsync(productToModify.ProductID, modifiedProduct);
                        };
                    webservice.FindAllProductsAsync();
                    //Buscamos el producto a modificar
                   
                };

                webservice.FindSellerFromNameAsync(Cookie.GetCookie("EncuentraloUser"));

            }
            else
            {
                MessageBox.Show("No se pudieron guardar los cambios");
            }

        }


        private void Salir(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Seller_View.xaml", UriKind.Relative));
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
        
        }
    }
}
